import os
import re
from pathlib import Path

# ==================== 配置选项 ====================
# 在这里修改配置，无需修改代码其他部分

# 提取的消息文件目录
EXTRACTED_DIR = r"C:\Users\Administrator\Desktop\1\extracted_messages"

# 输出目录
OUTPUT_DIR = r"C:\Users\Administrator\Desktop\1\filtered_sentence_messages"

# 是否显示详细进度
SHOW_DETAILED_PROGRESS = True

# 是否在输出文件名前添加前缀
ADD_PREFIX_TO_FILENAME = True

# 输出文件名前缀
FILENAME_PREFIX = "sentence_simple_"

# 是否显示文件大小信息
SHOW_FILE_SIZE = True

# ==================== 脚本代码 ====================

def extract_sentence_messages(input_file_path, output_file_path):
    """直接提取包含sentence的消息行"""
    sentence_lines = []
    line_count = 0
    
    print(f"开始处理文件: {input_file_path}")
    
    # 获取文件大小
    if SHOW_FILE_SIZE:
        try:
            file_size = os.path.getsize(input_file_path)
            print(f"文件大小: {file_size / (1024*1024):.2f} MB")
        except OSError as e:
            print(f"警告: 无法获取文件大小: {e}")
    
    try:
        with open(input_file_path, 'r', encoding='utf-8') as file:
            for line_num, line in enumerate(file, 1):
                line_count += 1
                line = line.strip()
                
                # 显示进度
                if SHOW_DETAILED_PROGRESS and line_count % 1000 == 0:
                    print(f"已处理 {line_count:,} 行，找到 {len(sentence_lines)} 条sentence消息...")
                
                # 查找包含msgtype为sentence的行
                if '"msgtype":"sentence"' in line:
                    sentence_lines.append({
                        'line_num': line_num,
                        'content': line
                    })
                    print(f"找到sentence消息 #{len(sentence_lines)} (第{line_num}行)")
        
        print(f"文件处理完成，总共处理了 {line_count:,} 行")
        print(f"找到 {len(sentence_lines)} 条sentence类型的消息")
        
        # 将筛选后的消息写入输出文件
        with open(output_file_path, 'w', encoding='utf-8') as output_file:
            output_file.write(f"从文件 {input_file_path} 提取的sentence类型消息\n")
            output_file.write(f"总共提取出 {len(sentence_lines)} 条sentence消息\n")
            output_file.write("=" * 50 + "\n\n")
            
            for i, item in enumerate(sentence_lines, 1):
                output_file.write(f"sentence消息 #{i} (第{item['line_num']}行):\n")
                output_file.write(item['content'])
                output_file.write("\n\n")
        
        print(f"提取结果已保存到: {output_file_path}")
        return len(sentence_lines)
        
    except FileNotFoundError:
        print(f"错误: 文件 {input_file_path} 不存在")
        return 0
    except PermissionError:
        print(f"错误: 没有权限访问文件 {input_file_path}")
        return 0
    except UnicodeDecodeError as e:
        print(f"错误: 文件编码问题 {input_file_path}: {e}")
        return 0
    except Exception as e:
        print(f"处理文件时出错: {e}")
        return 0

def main():
    # 显示当前配置
    print("当前配置:")
    print(f"  提取的消息文件目录: {EXTRACTED_DIR}")
    print(f"  输出目录: {OUTPUT_DIR}")
    print(f"  显示详细进度: {SHOW_DETAILED_PROGRESS}")
    print(f"  添加文件名前缀: {ADD_PREFIX_TO_FILENAME}")
    print(f"  文件名前缀: {FILENAME_PREFIX}")
    print(f"  显示文件大小: {SHOW_FILE_SIZE}")
    print()
    
    # 检查输入目录是否存在
    if not os.path.exists(EXTRACTED_DIR):
        print(f"错误: 目录 {EXTRACTED_DIR} 不存在")
        print("请先运行extract_log_messages.py提取消息")
        return
    
    # 创建输出目录
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    print(f"输出目录: {OUTPUT_DIR}")
    
    # 获取所有提取的消息文件
    try:
        extracted_files = [f for f in os.listdir(EXTRACTED_DIR) if f.endswith('.txt')]
    except PermissionError:
        print(f"错误: 没有权限访问目录 {EXTRACTED_DIR}")
        return
    except Exception as e:
        print(f"错误: 无法读取目录 {EXTRACTED_DIR}: {e}")
        return
    
    if not extracted_files:
        print("未找到提取的消息文件")
        print("请先运行extract_log_messages.py提取消息")
        return
    
    print(f"找到 {len(extracted_files)} 个提取的消息文件:")
    for file in extracted_files:
        file_path = os.path.join(EXTRACTED_DIR, file)
        try:
            if SHOW_FILE_SIZE:
                file_size = os.path.getsize(file_path) / (1024*1024)
                print(f"  - {file} ({file_size:.2f} MB)")
            else:
                print(f"  - {file}")
        except OSError:
            print(f"  - {file} (无法获取大小)")
    
    # 处理每个文件
    total_sentences = 0
    successful_files = 0
    
    for i, file in enumerate(extracted_files, 1):
        input_file_path = os.path.join(EXTRACTED_DIR, file)
        
        # 生成输出文件名
        if ADD_PREFIX_TO_FILENAME:
            output_file_name = f"{FILENAME_PREFIX}{file}"
        else:
            output_file_name = file
            
        output_file_path = os.path.join(OUTPUT_DIR, output_file_name)
        
        print(f"\n{'='*60}")
        print(f"正在处理: {file} ({i}/{len(extracted_files)})")
        print(f"{'='*60}")
        
        sentence_count = extract_sentence_messages(input_file_path, output_file_path)
        
        if sentence_count > 0:
            total_sentences += sentence_count
            successful_files += 1
            print(f"文件 {file} 成功提取了 {sentence_count} 条sentence消息")
        else:
            print(f"文件 {file} 没有提取到任何sentence消息")
    
    print(f"\n{'='*60}")
    print("所有文件处理完成！")
    print(f"提取的sentence消息保存在: {OUTPUT_DIR}")
    print(f"成功处理了 {successful_files}/{len(extracted_files)} 个文件")
    print(f"总共提取了 {total_sentences} 条sentence消息")
    print(f"{'='*60}")

if __name__ == "__main__":
    main()